import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { getabout,getcommend } from '../services/about';
import { List ,Commend} from '@/utils/about'
export interface AboutModelState {
  name: string;
  list: List[];
  commends:Commend[]
}
export interface IndexModelType {
  namespace: 'about';
  state: AboutModelState;
  effects: {
    query: Effect;
    getabout: Effect;
  };
  reducers: {
    save: Reducer<AboutModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'about',
  state: {
    list:[]   ,
    name: '柳轲',
    commends:[]
  },
  effects: {
    *query({ payload }, { call, put }) {},
    *getabout({ payload }, { call, put }) {
      //获取数据
      
      let result = yield call(getabout, payload);
      let commends=yield call(getcommend)
    
      yield put({
        type: 'save',
        payload: {
          list: result.data[0],
          commends:commends.data
        },
      });
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'query',
          });
        }
      });
    },
  },
};

export default IndexModel;
